package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.Agent;
import tech.aistar.entity.Register;
import tech.aistar.entity.Result;
import tech.aistar.service.IAgentService;
import tech.aistar.service.IRegisterService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/Agent")
public class AgentController {
    @Autowired
    private IAgentService agentService;

    @Autowired
    private IRegisterService registerService;

    @GetMapping("/info")
    public String select(HttpServletRequest request, Integer id) {
        String agentId = request.getParameter("agentId");
        Agent agent = agentService.selectById(Integer.valueOf(agentId));
        request.setAttribute("agent", agent);
        Register register = registerService.selectById(agent.getRegisterId());
        request.setAttribute("register", register);
        return "/personal_homepage/broker/infoB";
    }

    @PostMapping("/changepwd")
    public @ResponseBody
    Result updatepwd(Integer id, String password) {
        int count = registerService.updateById(id, password);

        if (count == 1) {
            return new Result("200", "修改成功", registerService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @PostMapping("/changephone")
    public @ResponseBody
    Result updatephone(String phone, Integer id) {
        int count = agentService.updatephoneById(phone, id);

        if (count == 1) {
            Result result = new Result("200", "修改成功", agentService.selectById(id));
            return result;
        } else {
            Result result = new Result("500", "修改失败");
            return result;
        }
    }


    @PostMapping("/changename")
    public @ResponseBody
    Result updatename(String name, Integer id) {

        int count = agentService.updateNameById(name, id);

        if (count == 1) {
            return new Result("200", "修改成功", agentService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @PostMapping("/changegender")
    public @ResponseBody
    Result updategender(String gender, Integer id) {

        int count = agentService.updategenderById(gender, id);

        if (count == 1) {
            return new Result("200", "修改成功", agentService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @PostMapping("/insertIdentity")
    public @ResponseBody Result insertIdentity(String identity,Integer id){

        int count = agentService.updateIdentityById(identity, id);

        if(count==1){
            return new Result("200","修改成功",agentService.selectById(id));
        }else{
            return new Result("500","修改失败");
        }
    }
}
